/// <license>
/// Copyright (c) 2002-2008, DotNetNuke Corporation
/// All rights reserved
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
/// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
/// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
/// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
/// of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
/// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
/// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
/// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
/// DEALINGS IN THE SOFTWARE.
/// </license>

#region Using Statements

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;

using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Users;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Portals;
using DotNetNuke.UI.WebControls;
using System.Xml.Serialization;
using System.Data;

#endregion

namespace DotNetNuke.ModuleToolkit.Framework
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The EntityBase Class provides a common Base class for all DotNetNuke Entities.
    /// </summary>
    /// <history>
    /// 	[cnurse]	01/31/2006	Created
    ///     [cnurse]    04/01/2006  Converted CreatedByUser and UpdatedByUser to ints
    ///     [cnurse]    12/22/2006  Moved to Dnn Library
    /// </history>
    /// -----------------------------------------------------------------------------
    public class EntityBase
    {

        #region Private Members

        private int _ModuleId;
        private int _ViewOrder;

        //Audit Info
        private int _CreatedByUser;
        private DateTime _CreatedDate;
        private int _UpdatedByUser;
        private DateTime _UpdatedDate;

        private UserInfo _CreatedBy;
        private UserInfo _UpdatedBy;

        #endregion

        #region Protected Properties

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the PortalId
        /// </summary>
        /// <history>
        /// 	[cnurse]	04/01/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(false), XmlIgnore()]
        protected int PortalId
        {
            get
            {
                PortalSettings settings = Globals.GetPortalSettings();
                int _PortalId = Null.NullInteger;
                if (settings != null)
                    _PortalId = settings.PortalId;

                return _PortalId;
            }
        }

        #endregion

        #region Public Properties

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets and sets the User that created the Entity
        /// </summary>
        /// <history>
        /// 	[cnurse]	01/31/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(false), XmlIgnore()]
        public int CreatedByUser
        {
            get { return _CreatedByUser; }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets and sets the Date that the Entity was created
        /// </summary>
        /// <history>
        /// 	[cnurse]	01/31/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(false), XmlIgnore()]
        public DateTime CreatedDate
        {
            get { return _CreatedDate; }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets and sets the Module Id of the Entity
        /// </summary>
        /// <history>
        /// 	[cnurse]	01/31/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(false), XmlIgnore()]
        public int ModuleId
        {
            get { return _ModuleId; }
            set { _ModuleId = value; }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets and sets the User that updated the Entity
        /// </summary>
        /// <history>
        /// 	[cnurse]	01/31/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(false), XmlIgnore()]
        public int UpdatedByUser
        {
            get { return _UpdatedByUser; }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets and sets the Date that the Entity was created
        /// </summary>
        /// <history>
        /// 	[cnurse]	01/31/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(false), XmlIgnore()]
        public DateTime UpdatedDate
        {
            get { return _UpdatedDate; }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets and sets the ViewOrder of the Entity
        /// </summary>
        /// <history>
        /// 	[cnurse]	01/31/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(true), Category("Basic"), SortOrder(997), XmlElement("viewOrder")]
        public int ViewOrder
        {
            get { return _ViewOrder; }
            set { _ViewOrder = value; }
        }

        #endregion

        #region Protected Methods

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Hydrates an Entity from a DataReader representing the Entity
        /// </summary>
        /// <param name="entity">The Entity object to fill</param>
        /// <param name="source">The Data Reader containing the values to use to fill the Entity.</param>
        /// <history>
        /// 	[cnurse]	12/19/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected virtual void FillInternal(IDataReader source)
        {
            ModuleId = Convert.ToInt32(Null.SetNull(source["ModuleID"], ModuleId));
            ViewOrder = Convert.ToInt32(Null.SetNull(source["ViewOrder"], ViewOrder));
            _CreatedByUser = Convert.ToInt32(Null.SetNull(source["CreatedByUser"], _CreatedByUser));
            _UpdatedByUser = Convert.ToInt32(Null.SetNull(source["UpdatedByUser"], _UpdatedByUser));
            _CreatedDate = Convert.ToDateTime(Null.SetNull(source["CreatedDate"], _CreatedDate));
            _UpdatedDate = Convert.ToDateTime(Null.SetNull(source["UpdatedDate"], _UpdatedDate));
        }

        #endregion

        #region Public Methods

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the CreatedBy User Info
        /// </summary>
        /// <history>
        /// 	[cnurse]	04/01/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public UserInfo GetCreatedBy()
        {
            if (_CreatedBy == null)
            {
                _CreatedBy = UserController.GetUser(PortalId, CreatedByUser, false);
            }

            return _CreatedBy;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the UpdatedBy User Info
        /// </summary>
        /// <history>
        /// 	[cnurse]	04/01/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public UserInfo GetUpdatedBy()
        {
            if (_UpdatedBy == null)
            {
                _UpdatedBy = UserController.GetUser(PortalId, UpdatedByUser, false);
            }

            return _UpdatedBy;
        }

        #endregion

    }
}
